﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace jQueryDemo.Class
{
    public class PageBase : Page
    {
        protected List<string> JScripts { get; set; }
        protected string ClassName
        {
            get
            {
                string[] classParts = GetType().ToString().Split('.');
                return classParts[classParts.Length - 1].Split('_')[0];
            }
        }
        protected string ThemeName
        {
            get
            {
                return (Session["App_Theme"] == null) ? "Redmond" : Session["App_Theme"].ToString();
            }
            set
            {
                Session["App_Theme"] = value;
            }
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            JScripts = new List<string>();
            JScripts.Add("jquery-1.6.1.min.js");
            JScripts.Add("jquery-ui-1.8.13.min.js");
            JScripts.Add("jquery-ui-timepicker-addon.js");
            JScripts.Add("jquery.blockUI.js");
            JScripts.Add("jquery.color.js");
            JScripts.Add("jquery.easing.1.3.js");
            JScripts.Add("jquery.gritter.min.js");
        }
        protected virtual void Page_Load(object sender, EventArgs e)
        {
            JScripts.Add("jquery.hn.extend.js");
            JScripts.Add(string.Format("Page/{0}.js", ClassName));
            //Register stuff
            registerJScript();
            registerCSS();
        }
        private void registerJScript()
        {
            foreach (string scriptName in JScripts)
            {
                Page.ClientScript.RegisterClientScriptInclude(scriptName, Page.ResolveUrl(string.Format("~/JScript/{0}", scriptName)));
            }
        }
        private void registerCSS()
        {
            //Page CSS
            HtmlLink cssLink = new HtmlLink();
            cssLink.Href = string.Format("~/CSS/{0}.css", ClassName);
            cssLink.Attributes.Add("rel", "stylesheet");
            cssLink.Attributes.Add("type", "text/css");
            Page.Header.Controls.Add(cssLink);

            //Theme CSS
            HtmlLink themeLink = new HtmlLink();
            themeLink.Href = string.Format("~/CSS/Themes/{0}/Theme.css", ThemeName);
            themeLink.Attributes.Add("rel", "stylesheet");
            themeLink.Attributes.Add("type", "text/css");
            Page.Header.Controls.Add(themeLink);
        }
    }
}